package ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControllPrinter;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class PrinterSpezPanel extends JPanel implements ActionListener, ChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1957537454850488931L;
	private JCheckBox printerName;
	private JCheckBox printerDriverName;
	private JCheckBox printerDriverVersion;
	private VisuControllPrinter visuControllPrinter;
	private int amountOfFlags;

	public PrinterSpezPanel(VisuControllPrinter visuControllPrinter) {
		this.visuControllPrinter = visuControllPrinter;
		this.amountOfFlags = 0;
		init();
	}

	public void init() {
		this.printerName = new JCheckBox("Drucker-Name", this.visuControllPrinter.isPrinterName());
		this.printerName.addActionListener(this);
		this.printerName.addChangeListener(this);
		this.printerDriverName = new JCheckBox("Treiber-Name", this.visuControllPrinter.isPrinterDriverName());
		this.printerDriverName.addActionListener(this);
		this.printerDriverName.addChangeListener(this);
		this.printerDriverVersion = new JCheckBox("Treiber-Version", this.visuControllPrinter.isPrinterDriverVersion());
		this.printerDriverVersion.addActionListener(this);
		this.printerDriverVersion.addChangeListener(this);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		double checkBox = 0.0;
		double lable = 1.0;
		gridBagLayout.columnWeights = new double[] { checkBox, checkBox, checkBox, lable };
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		int widthCheckbox = SettingsPanel.WIDTH_CHECKBOX;
		gridBagLayout.columnWidths = new int[] { widthCheckbox, widthCheckbox, widthCheckbox, 0 };
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		int x = 0;
		int y = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.printerName);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.printerDriverName);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.printerDriverVersion);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
	}

	public void setValueToAllCheckBoxes(boolean flag) {
		this.printerName.setSelected(flag);
		this.printerDriverName.setSelected(flag);
		this.printerDriverVersion.setSelected(flag);
	}

	public void actionPerformed(ActionEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.printerName) {
			this.visuControllPrinter.setPrinterName(this.printerName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.printerDriverName) {
			this.visuControllPrinter.setPrinterDriverName(this.printerDriverName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.printerDriverVersion) {
			this.visuControllPrinter.setPrinterDriverVersion(this.printerDriverVersion.isSelected());
			checkCanCrement();
		}
	}

	public void stateChanged(ChangeEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.printerName) {
			this.visuControllPrinter.setPrinterName(this.printerName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.printerDriverName) {
			this.visuControllPrinter.setPrinterDriverName(this.printerDriverName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.printerDriverVersion) {
			this.visuControllPrinter.setPrinterDriverVersion(this.printerDriverVersion.isSelected());
			checkCanCrement();
		}
	}

	public void checkCanCrement() {
		if (canCrement()) {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(true);
		} else {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(false);
		}
	}

	public boolean canCrement() {
		return this.amountOfFlags < 8;
	}

	public boolean getPrinterNameState() {
		return this.printerName.isSelected();
	}

	public boolean getPrinterDriverNameState() {
		return this.printerDriverName.isSelected();
	}

	public boolean getPrinterDriverVersionState() {
		return this.printerDriverVersion.isSelected();
	}

	public void setAllEnabledStates(boolean beEnabledFlag) {
		if (!this.printerName.isSelected()) {
			this.printerName.setEnabled(beEnabledFlag);
		}
		if (!this.printerDriverName.isSelected()) {
			this.printerDriverName.setEnabled(beEnabledFlag);
		}
		if (!this.printerDriverVersion.isSelected()) {
			this.printerDriverVersion.setEnabled(beEnabledFlag);
		}

	}
}
